package top.hudk.tank.frame;

import top.hudk.tank.config.GameConfig;
import top.hudk.tank.constant.GameConstant;
import top.hudk.tank.enums.Dir;
import top.hudk.tank.enums.Group;
import top.hudk.tank.factory.*;
import top.hudk.tank.model.GameApplicationContext;
import top.hudk.tank.model.GameObject;
import top.hudk.tank.strategy.SingleShotFireStrategy;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 作用：游戏主窗口类
 *
 * @author hudk
 * @date 2020/9/8 14:24
 */
public class TankFrame extends AbstractGameFrame {

    public TankFrame() {
        super.setSize(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT);
        super.setResizable(false);
        super.setTitle(GameConstant.GAME_NAME);
        //监听键盘按键事件
        super.addKeyListener(new MyKeyListener());
        //监听主窗口被关闭事件
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        gameApplicationContext = GameApplicationContext.getInstance();
    }

    /**
     * 绘制画面内容
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        gameApplicationContext.paint(g, this);
    }


    /**
     * 键盘监听内部类
     */
    class MyKeyListener extends KeyAdapter {

        /**
         * 键盘方向
         */
        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        /**
         * 键盘按键按下事件
         *
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            //获取到按键信息
            int key = e.getKeyCode();
            switch (key) {
                //向左键
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                //向上键
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                //向右键
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                //向下键
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_CONTROL:
                    break;
                default:
                    break;
            }
            setMyTankDir();
        }

        /**
         * 键盘按键抬起事件
         *
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    //System.out.println("- ←");
                    //setMyTankDir();
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    //System.out.println("- ↑");
                    //setMyTankDir();
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    //System.out.println("- →");
                    //setMyTankDir();
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    //System.out.println("- ↓");
                    //setMyTankDir();
                    break;
                case KeyEvent.VK_CONTROL:
                    //System.out.println("- c");
                    gameApplicationContext.getMyTank().fire(SingleShotFireStrategy.getInstance());
                    break;
                default:
                    break;
            }
            setMyTankDir();
//            if (fire) {
//                gm.getMyTank().fire(SingleShotFireStrategy.getInstance());
//                fire = false;
//            }
        }

        private void setMyTankDir() {

            Tank myTank = gameApplicationContext.getMyTank();
            if (bL) {
                myTank.setMoving(true);
                myTank.setDir(Dir.LEFT);
                //System.out.println("m ←");
            } else if (bU) {
                myTank.setMoving(true);
                myTank.setDir(Dir.UP);
                // System.out.println("m ↑");
            } else if (bR) {
                myTank.setMoving(true);
                myTank.setDir(Dir.RIGHT);
                //System.out.println("m →");
            } else if (bD) {
                myTank.setMoving(true);
                myTank.setDir(Dir.DOWN);
                //System.out.println("m ↓");
            } else {
                myTank.setMoving(false);
                //System.out.println("stop");
            }

        }
    }

    @Override
    public int maxX(GameObject o) {
        return getGameWidth() - o.getWidth() - GameConfig.PADDING_RIGTH;
    }

    @Override
    public int minX() {
        return GameConfig.PADDING_LEFT;
    }

    @Override
    public int maxY(GameObject o) {
        return getGameHeight() - o.getHeight() - GameConfig.PADDING_DOWN;
    }

    @Override
    public int minY() {
        return GameConfig.PADDING_UP;
    }

}
